﻿# CoffeeScript
if not window.Moshu
    window.Moshu = new Object()          

class Moshu.Canvas2D.CanvasImage extends Moshu.Canvas2D.Rectangle
    constructor : (@src,@x,@y,@width,@height) ->
        super(@x,@y,@width,@height)        
        @image = new Image()
        @image.src = @src
        @sprites = []
        @currentSprite = null
        @variableWidth = true        
    registerSprite : (name,x,y,width,height)->
        sprite = 
            name : name
            x : x
            y : y
            width : width
            height : height
        @sprites.push(sprite)
    selectSprite : (sprite)->
        @currentSprite = sprite
        if @variableWidth
            @width = @height / sprite.height * sprite.width
        @currentSprite
    selectSpriteByIndex : (idx) ->        
        if idx<0             
            idx = idx % @sprites.length
            idx = @sprites.length + idx
        if idx>=@sprites.length
            idx = idx % @sprites.length
        @selectSprite(@sprites[idx])        
    selectSpriteByName : (name)->
        for s, i in @sprites
            if s.name is name
                @selectSprite(s)
                break                
        @currentSprite
    selectNextSprite : ()->        
        if @currentSprite
            for s, i in @sprites
                if s is @currentSprite
                    @selectSpriteByIndex(i+1)
                    break
        else
            @selectSpriteByIndex(0)
        @currentSprite
    selectPrevSprite : ()->        
        if @currentSprite
            for s, i in @sprites
                if s is @currentSprite
                    @selectSpriteByIndex(i-1)
                    break
        else
            @selectSpriteByIndex(0)
        @currentSprite
    
class Moshu.Canvas2D.ImagePainter extends Moshu.Canvas2D.Painter
   constructor : (@context, @canvasImage) ->
        super
        @scale = new Moshu.Canvas2D.Vector(1,1)
    flipHorizontally : ()->
        @scale.x(@scale.x()*-1)
    flipVertically : ()->
        @scale.y(@scale.y()*-1)        
    resetFlip: ()->    
        @scale = new Moshu.Canvas2D.Vector(Math.abs(@scale.x()),Math.abs(@scale.y()))
    draw : () ->
        if @context and @canvasImage        
            posX = @canvasImage.x
            posY = @canvasImage.y
            if @scale.x()<0
                posX = @canvasImage.x * @scale.x()+@canvasImage.width* @scale.x()
            if @scale.y()<0
                posY = @canvasImage.y * @scale.y()+@canvasImage.height* @scale.y()
            
            @context.save()
            #@context.translate(@canvasImage.x,@canvasImage.y)
            @context.scale(@scale.x(), @scale.y())
            if @canvasImage.currentSprite
                @context.drawImage(@canvasImage.image, @canvasImage.currentSprite.x,@canvasImage.currentSprite.y,@canvasImage.currentSprite.width,@canvasImage.currentSprite.height,posX,posY,@canvasImage.width,@canvasImage.height)                
            else @context.drawImage(@canvasImage.image, posX, posY,@canvasImage.width,@canvasImage.height)
            @context.restore()
            
            
